const express = require('express');
const { json } = require('express/lib/response');
const router = express.Router();
const db = require('../../utils/db')

//今日订单
router.get('/gettodayorder', function (req, res, next) {
  let mysql = `SELECT order_id,order_num,customer_name,order_time,customer_type,cash_money,pay_way,table_num,order_state,order_del_state FROM order_information 
  JOIN customer ON order_information.order_username=customer.customer_id
  JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
  JOIN cash_flowing ON order_information.order_id=cash_flowing.cash_num
  JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id
  WHERE order_information.order_time like ?
  Group by order_information.order_num`


  db(mysql, [req.query.date + '%'], function (err, data) {
    if (err) {
      return res.json({
        code: 500,
        msg: '查询今日订单失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '查询今日订单成功',
      data: data
    })

  })
});

//模糊查询
router.get('/getorderid', function (req, res) {
  let mysql = `SELECT order_id,order_num,customer_name,order_time,customer_type,cash_money,pay_way,table_num,order_state,order_del_state FROM order_information 
             JOIN customer ON order_information.order_username=customer.customer_id
             JOIN pay_method ON order_information.order_pay_way=pay_method.pay_id
             JOIN cash_flowing ON order_information.order_id=cash_flowing.cash_num
             JOIN table_area ON cash_flowing.cash_table_area=table_area.table_id
             WHERE order_information.order_time like '${req.query.date2}%' 
             `
  if (JSON.parse(req.query.data1).orderId) {
    mysql += `AND order_num LIKE '%${JSON.parse(req.query.data1).orderId}%' `
  }
  if (JSON.parse(req.query.data1).payWay) {
    mysql += `AND pay_way='${JSON.parse(req.query.data1).payWay}' `
  }
  mysql += `GROUP BY order_num`
  db(mysql, null, function (err, data) {
    console.log(err);
    if (err) {
      return res.json({
        code: 500,
        msg: '模糊查询历史订单失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '模糊查询历史订单成功',
      data: data
    })
  })
})

//删除今日订单
router.post('/delhistoryorder', function (req, res) {
  let mysql = 'UPDATE order_information SET order_del_state=0 WHERE order_id=?';
  db(mysql, [req.body.order_id], (err, data) => {
    if (err) {
      return res.json({
        code: 500,
        msg: '删除订单失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '删除订单成功',
      data: data
    })
  })
})

//// 修改今日订单状态
router.post('/updatehistoryorderstate', function (req, res) {
  let mysql = `UPDATE order_information SET order_state='未付款' WHERE order_id=?`;
  db(mysql, [req.body.order_id], (err, data) => {
    if (err) {
      return res.json({
        code: 500,
        msg: '修改订单状态失败，请稍后重试'
      })
    }
    res.json({
      code: 200,
      msg: '修改订单状态成功',
      data: data
    })
  })
})



module.exports = router